//
//  SalesGoodsListViewController.m
//  JiuJiuShangCheng
//
//  Created by jiujiukeji on 2018/7/6.
//  Copyright © 2018年 jiujiukeji. All rights reserved.
//


#import "WenDaViewController.h"
#import "WenDaTableViewCell.h"
#import "CommentModel.h"

@interface WenDaViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger pageSize;

@end

@implementation WenDaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = UIColorWhite;
    [self loadBaseTitleLable:@"问答"];
    
    self.currentPage = 1;
    self.pageSize = 20;
    
    [self setUpUI];
    [self getGoodsListData];
}
#pragma mark - 评价列表
- (void)getGoodsListData
{
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/common/common.ashx?action=commentlist&companyid=%@&id=%@&pagesize=%@&pageindex=1",kServerURL,companyId,self.goodsId,[NSString stringWithFormat:@"%ld",(long)self.pageSize]] parameters:nil success:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:self.view];
        self.dataArray = [NSMutableArray array];
        self.currentPage = 1;
        if ([responseObject[@"status"] intValue] == 1) {
            self.dataArray = [CommentModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        }
        NSInteger i = [responseObject[@"count"] intValue]/self.pageSize;
        NSInteger j = [responseObject[@"count"] intValue]%self.pageSize;
        NSInteger n = 0;
        if (j > 0) {
            n = i+1;
        } else {
            n = i;
        }
        if (n<2) {
            self.tableView.mj_footer.hidden = YES;
        } else {
            self.tableView.mj_footer.hidden = NO;
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)getGoodsListDataMore
{
    self.currentPage++;
    [MBProgressHUD showMessage:nil toView:self.view];
    [TFNetworking getWithURLString:[NSString stringWithFormat:@"%@/api/json/common/common.ashx?action=commentlist&companyid=%@&id=%@&pagesize=%@&pageindex=%@",kServerURL,companyId,self.goodsId,[NSString stringWithFormat:@"%ld",(long)self.pageSize],[NSString stringWithFormat:@"%ld",(long)self.currentPage]] parameters:nil success:^(id responseObject) {
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD hideHUDForView:self.view];
        if ([responseObject[@"status"] intValue] == 1) {
            
            NSInteger i = [responseObject[@"count"] intValue]/self.pageSize;
            NSInteger j = [responseObject[@"count"] intValue]%self.pageSize;
            NSInteger n = 0;
            if (j > 0) {
                n = i+1;
            } else {
                n = i;
            }
            if (self.currentPage > n) {
                self.currentPage--;
                self.tableView.mj_footer.hidden = YES;
            } else {
                self.tableView.mj_footer.hidden = NO;
                self.dataArray = (NSMutableArray *)[self.dataArray arrayByAddingObjectsFromArray:[CommentModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]];
                [self.tableView reloadData];
            }
        } else {
            self.currentPage--;
            self.tableView.mj_footer.hidden = YES;
        }
        
    } failure:^(NSError *error) {
        [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        [self.tableView.mj_footer endRefreshing];
    }];
}
- (void)setUpUI
{
    [self.view addSubview:self.tableView];
    
    UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight-rateHeight(100), kScreenWidth, rateHeight(100))];
    bottomV.backgroundColor = UIColorWhite;
    [self.view addSubview:bottomV];
    
    UITextField *textField = [[UITextField alloc] init];
    textField.font = UIFontSystem(14);
    textField.textColor = UIColorFromRGB(0x333333);
    textField.placeholder = @"请输入您要问的问题...";
    [bottomV addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bottomV).offset(rateWidth(30));
        make.size.mas_offset(CGSizeMake(kScreenWidth-rateWidth(30+150), rateHeight(80)));
        make.centerY.equalTo(bottomV);
    }];
    self.textField = textField;
    
    UIButton *btn = [UIButton buttonWithTitle:@"发送" fontSize:14 titleColor:UIColorWhite background:navBarColor cornerRadius:0];
    [bottomV addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(rateWidth(150), rateHeight(100)));
        make.right.centerY.equalTo(bottomV);
    }];
    [btn addTarget:self action:@selector(sendAction) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = globalGray;
    [bottomV addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(bottomV);
        make.size.mas_offset(CGSizeMake(kScreenWidth-rateWidth(150), rateHeight(1)));
    }];
}
#pragma mark - 发送
- (void)sendAction
{
    if (self.textField.text.length != 0) {
        NSString *code = [NSString stringWithFormat:@"%@%@",safeCode,[GetTimestamp getNowTimeTimestamp]];
        [MBProgressHUD showMessage:nil toView:self.view];
        [TFNetworking postWithURLString:[NSString stringWithFormat:@"%@/api/json/common/common.ashx?action=commentadd&companyid=%@&code=%@&timestamp=%@",kServerURL,companyId,[MD5Encrypt  MD5ForLower32Bate:code],[GetTimestamp getNowTimeTimestamp]] parameters:@{@"id":self.goodsId,@"commentcontent":self.textField.text} success:^(id responseObject) {
            [MBProgressHUD hideHUDForView:self.view];
            if (responseObject) {
                [TFHUD addRemindHUD:self.view message:responseObject[@"returnMsg"]];
                if ([responseObject[@"status"] intValue] == 1) {
                    [self.textField resignFirstResponder];
                    [self getGoodsListData];
                }
            }
        } failure:^(NSError *error) {
            NSLog(@"%@",error);
            [TFHUD addRemindHUD:self.view message:@"网络不给力..."];
        }];
    }
}
#pragma mark - tableView delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WenDaTableViewCell *cell = [[WenDaTableViewCell alloc] init];
    
    cell.selectionStyle = NO;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return rateHeight(80);
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return rateHeight(80);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *bgV = [[UIView alloc] init];
    bgV.backgroundColor = UIColorWhite;
    UIImageView *img = [[UIImageView alloc] init];
    img.image = UIImageNamed(@"wenti");
    [img sizeToFit];
    [bgV addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgV).offset(rateWidth(30));
        make.centerY.equalTo(bgV);
    }];
    UILabel *textLB = [UILabel labelWithText:@"这是一个问题" textColor:UIColorFromRGB(0x333333) fontSize:14];
    [textLB sizeToFit];
    textLB.numberOfLines = 0;
    [bgV addSubview:textLB];
    [textLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img.mas_right).offset(rateWidth(10));
        make.centerY.equalTo(bgV);
        make.width.mas_lessThanOrEqualTo(rateWidth(600));
    }];
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = globalGray;
    [bgV addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(bgV);
        make.height.equalTo(@(rateHeight(2)));
    }];
    // 赋值
//    if (_dataArray.count != 0) {
//        CommentModel *model = _dataArray[section];
//        textLB.text = model.commentContent;
//    }
    
    return bgV;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight, kScreenWidth, kScreenHeight-SafeAreaTopHeight-rateHeight(100)) style:(UITableViewStyleGrouped)];
        _tableView.backgroundColor = UIColorWhite;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = NO;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        WeakSelf
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakSelf getGoodsListData];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [weakSelf getGoodsListDataMore];
        }];
    }
    return _tableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
